package com.eastmoney.emis.edda.infrastructure.hsbc.interceptor;

import com.eastmoney.emis.edda.config.HsbcEddaConfig;
import com.eastmoney.emis.logger.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.interceptor
 * @author: 汪程程
 * @date: 2022/9/26 5:18 PM
 * @version: 1.0
 */
public class HsbcEddaRequestInterceptor implements ClientHttpRequestInterceptor {
    private static final Logger log = LoggerFactory.getLogger(HsbcEddaRequestInterceptor.class);
    private HsbcEddaConfig hsbcEddaConfig;

    public HsbcEddaRequestInterceptor() {
    }

    public HsbcEddaRequestInterceptor(HsbcEddaConfig hsbcEddaConfig) {
        this.hsbcEddaConfig = hsbcEddaConfig;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        HttpHeaders headers = request.getHeaders();
        headers.add("x-hsbc-profile-id", hsbcEddaConfig.getProfileId());
        headers.add("x-hsbc-client-id", hsbcEddaConfig.getClientId());
        headers.add("x-hsbc-client-secret", hsbcEddaConfig.getClientSecret());
        headers.add("x-hsbc-country-code", hsbcEddaConfig.getCountryCode());

        ClientHttpResponse execute = execution.execute(request, body);
        return execute;
    }


}
